package com.atguigu.gmall.realtime.publisher.controller;

import com.atguigu.gmall.realtime.publisher.bean.TrafficUvCt;
import com.atguigu.gmall.realtime.publisher.service.TrafficStatsService;
import com.atguigu.gmall.realtime.publisher.util.DateFormatUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Felix
 * @date 2022/8/31
 * 流量域Controller
 */
@RestController
public class TrafficController {

    @Autowired
    private TrafficStatsService trafficStatsService;

    @RequestMapping("/ch")
    public Map getChUvCt(
        @RequestParam(value = "date",defaultValue = "0") Integer date,
        @RequestParam(value = "limit",defaultValue = "10") Integer limit){

        if(date == 0){
            date = DateFormatUtil.now();
        }


        Map resMap = new HashMap();
        resMap.put("status",0);
        List dataList = new ArrayList();
        List<TrafficUvCt> uvCtList = trafficStatsService.getChUvCt(date, limit);
        for (TrafficUvCt trafficUvCt : uvCtList) {
            Map dataMap = new HashMap();
            dataMap.put("name",trafficUvCt.getCh());
            dataMap.put("value",trafficUvCt.getUvCt());
            dataList.add(dataMap);
        }
        resMap.put("data",dataList);
        return resMap;
    }
}
